<template>
    <div class="phoneNumber">
        <!--#ifndef MP-WEIXIN-->
        <div @click.stop="nextStep(dataSource,`?owner_user_id=${user_id}`)">
            <slot></slot>
        </div>
        <!-- #endif-->
        <!--#ifdef MP-WEIXIN-->
        <div @click.stop="getPhoneNumber(postE)" v-if="phoneNumber" class="no-get-phone-btn">
            <slot></slot>
        </div>
        <button class="buttonPlain" v-else open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" :plain="true" hover-class="none" :data-source="dataSource">
            <slot></slot>
        </button>
        <!-- #endif-->
    </div>
</template>

<script>
import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
import { getUserInfo, getWxUserInfo } from '@/api/login'
import { getBasicUser } from '@/utils/auth'

export default {
	name: 'getPhoneNumber',
	props: {
		dataSource: {
			type: String,
			default: ''
		},
		itemIndex: {
			type: Number,
			default: 0
		}
	},
	data() {
		return {
			postE: {
				type: 'hasPhoneNumber',
				phoneNumber: null,
				page: this.dataSource
			},
			mobile: '',
			currentQuery: {},
			user_id: ''
		}
	},
	created() {
		this.currentQuery = JSON.parse(uni.getStorageSync('currentQuery'))
	// 	getBasicUser(this.currentQuery.owner_user_id || uni.getStorageSync('user_id')).then(res => {
	// 		this.user_id = res.user_id
	// 	})
	},
	computed: {
		...mapGetters({ userInfo: 'getBasicUser' }),
		...mapState({
			loginInfo: state => state.login.loginInfo,
			authorization: state => state.authorization.authorization,
			phoneNumber: state => state.authorization.authorization.phoneNumber
		})
		// authorization () {
		// 	let wxMobile = uni.getStorageSync('wxMobile') ? true : false
		// 	console.log(wxMobile)
		// 	return wxMobile
		// }
	},
	mounted() {
		const wxMobile = uni.getStorageSync('wxMobile') || null
		// this.postE.phoneNumber = wxMobile ? wxMobile : ''
		// if (wxMobile) {
		this.setAuthorization({ phoneNumber: wxMobile })
		// }
		this.postE.phoneNumber = this.authorization ? this.authorization.phoneNumber : null
	},
	methods: {
		...mapActions(['setAuthorization']),
		getPhoneNumber(e) {
			e.itemIndex = this.itemIndex
			let that = this
			console.log(e, '用户信息')
			if (e.type === 'getphonenumber') {
				if (e.mp.detail.errMsg === 'getPhoneNumber:fail user deny' || !e.mp.detail.iv) {
					let queryData = ``
					const source = e.currentTarget.dataset.source
					if (source === '保存名片' || source === 'wxAuth') {
						return
					}
					that.gotoPage(e.currentTarget.dataset.source, queryData)
				} else {
					let data = {
						sessionKey: that.loginInfo.session_key,
						iv: e.detail.iv,
						encryptData: e.detail.encryptedData
					}
					this.postRequest(e.currentTarget.dataset.source, data).then(res => {
						that.mobile = res.data.phoneNumber
						that.setAuthorization({ phoneNumber: that.mobile }).then(() => {
							let queryData = `?mobile=${that.mobile}`
                			uni.setStorageSync('wxMobile', that.mobile)
							that.gotoPage(e.currentTarget.dataset.source, queryData)
						})
					})
				}
			} else if (e.type === 'hasPhoneNumber') {
				let queryData = `?mobile=${that.mobile}`
				that.gotoPage(e.page, queryData)
			}
		},
		gotoPage(pageInfo, queryData) {
			// if (that.menuList[e.itemIndex].function) {
			// 	that.menuList[e.itemIndex].function()
			// }
			if (pageInfo === 'wxAuth') {
				this.$emit('cbFn', pageInfo)
				return
			}
			const returnUrl = `${this.commonJS.queryData({
				owner_user_id: this.userInfo.user_id,
				type: 'hkds_miniapp',
				// token: uni.getStorageSync('token'),
				mobile: this.mobile
			})}`
			let params
			if (queryData) {
				params = `${queryData}&owner_user_id=${this.currentQuery.owner_user_id || this.userInfo.user_id}`
			} else {
				params = `?owner_user_id=${this.currentQuery.owner_user_id || this.userInfo.user_id}`
			}
			this.nextStep(pageInfo, params)
		},
		postRequest(source, params) {
			if (source === 'wxAuth') {
				return getWxUserInfo(params)
			}
			return getUserInfo(params)
		},
		nextStep(pageInfo, params) {
			switch (pageInfo) {
				case 'fenshui':
					uni.navigateTo({
						// url: `/pages/page/webview/index?user_id=${this.user_id}&returnUrl=${encodeURIComponent(`${this.$constant.formal}/hkds/#/company/prebook/fengshui${returnUrl}`)}`
						url: `/pages/pagesHome/shop/ShopDivination${params}`
					})
					break
				case '智能报价':
					uni.navigateTo({
						// url: `/pages/page/webview/index?user_id=${this.user_id}&returnUrl=${encodeURIComponent(`${this.$constant.formal}/hkds/#/company/prebook/price${returnUrl}`)}`
						url: `/pages/pagesHome/thematicmo/inteoffer${params}`
					})
					break
				case '户型设计':
					uni.navigateTo({
						// url: `/pages/page/webview/index?user_id=${this.user_id}&returnUrl=${encodeURIComponent(`${this.$constant.formal}/hkds/#/company/prebook/huxing${returnUrl}`)}`
						url: `/pages/pagesHome/active/houseDesign${params}`
					})
					break
				case '装修红包':
					this.$parent.$refs.appCouponPup.showMask(this.mobile)
					break
				case '保存名片':
					this.$parent.showCardMask()
					break
				default:
					this.$emit('cbFn', pageInfo)
					break
			}
		}
	}
}
</script>
<style scoped lang="scss">
.phoneNumber {
    .buttonPlain {
        border: none !important;
        padding: 0;
        margin: 0;
        line-height: inherit;
        width: auto;
        height: auto;
    }
    button {
        font-size: none;
    }
}
</style>
